package com.yygo.webapp.controller;

import com.yygo.model.LoanPerson;
import com.yygo.model.LoanRequest;
import com.yygo.report.JavaBeanPerson;
import com.yygo.service.LoanRequestManager;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by tiansha on 2015/7/21.
 */
@Controller
public class ReportController extends BaseFormController {

    @Autowired
    private LoanRequestManager loanRequestManager;

    @RequestMapping(value = "/report_test", method = RequestMethod.GET)
    public String reportTest(Model model) {
        // 报表数据源
        JRDataSource jrDataSource = new JRBeanCollectionDataSource(JavaBeanPerson.getList());

        // 动态指定报表模板url
        //model.addAttribute("url", "/WEB-INF/jasper/sample_report.jasper");
        model.addAttribute("format", "pdf"); // 报表格式
        model.addAttribute("requestDate", new Date());
        model.addAttribute("jrMainDataSource", jrDataSource);

        return "reportView"; // 对应jasper-views.xml中的bean id
    }

    @RequestMapping(value = "/personList", method = RequestMethod.GET)
    public String personList(Model model, HttpServletRequest request, @RequestParam(required = true, value = "reqId") Long reqId) {
        LoanRequest loanRequest = loanRequestManager.get(reqId);
        LoanPerson requestPerson = loanRequest.getRequestPerson();
        model.addAttribute("loanRequest", loanRequest);
        model.addAttribute("requestName", requestPerson.getName());
        model.addAttribute("requestType", getText("ID_TYPE_" + requestPerson.getIdType(), request.getLocale()));
        model.addAttribute("requestNo", requestPerson.getIdNo());
        LoanPerson coPerson = loanRequest.getCoPerson();
        if (coPerson != null) {
            model.addAttribute("coName", coPerson.getName());
            model.addAttribute("coType", getText("ID_TYPE_" + coPerson.getIdType(), request.getLocale()));
            model.addAttribute("coNo", coPerson.getIdNo());
        } else {
            model.addAttribute("coName", "");
            model.addAttribute("coType", "");
            model.addAttribute("coNo", "");
        }
        LoanPerson dbPerson = loanRequest.getDbPerson();
        if (dbPerson != null) {
            model.addAttribute("dbName", dbPerson.getName());
            model.addAttribute("dbType", getText("ID_TYPE_" + dbPerson.getIdType(), request.getLocale()));
            model.addAttribute("dbNo", dbPerson.getIdNo());
        } else {
            model.addAttribute("dbName", "");
            model.addAttribute("dbType", "");
            model.addAttribute("dbNo", "");
        }
        model.addAttribute("number", loanRequest.getNumber());
        model.addAttribute("format", "pdf");
        //model.addAttribute("url", "/WEB-INF/jasper/personList.jasper");
        return "personList";
    }

    @RequestMapping(value = "/batchExport", method = RequestMethod.GET)
    public String batchExport(Model model, HttpServletRequest request, @RequestParam(required = true, value = "selectedIds") Long[] selectedIds) {
        List<LoanRequest> loanRequestList = new ArrayList();
        for (Long id : selectedIds) {
            loanRequestList.add(loanRequestManager.get(id));
        }
        // 报表数据源
        JRDataSource jrDataSource = new JRBeanCollectionDataSource(loanRequestList);

        // 动态指定报表模板url
        //model.addAttribute("url", "/WEB-INF/jasper/sample_report.jasper");
        model.addAttribute("format", "pdf"); // 报表格式 xls
        model.addAttribute("jrMainDataSource", jrDataSource);

        return "batchExport";
    }

}
